package cn.northsea.utils;

import cn.northsea.entity.SeekUser;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    public static String createJwt(SeekUser seekUser) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userid", seekUser.getUserId());
        map.put("role",seekUser.getTenantId());
        map.put("username",seekUser.getUsername());
        // 生成jwt
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "jwtKey") //签名算法
                .setClaims(map)                                 //自定义载荷
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 3)) //有效期3h
                // .setExpiration(new Date(System.currentTimeMillis() + 1000)) //有效期1s
                .compact();
    }

    // 解析jwt
    public static Claims parseJwt(String jwt){
        return Jwts.parser()
                .setSigningKey("jwtKey") //指定编码签名
                .parseClaimsJws(jwt) // 传递jwt字符串,解析令牌
                .getBody();
    }
}
